<template>
    <div>
        <el-divider content-position="left">您具有一定的操作权限</el-divider>
    </div>

    <div>
        <el-divider>
            <span id="spanlittlepart">查询</span>
        </el-divider>
    </div>

    <!-- 查询 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="管理员名称" style="width: 200px;">
            <el-input v-model="checkformInline.adminename" placeholder="管理员"></el-input>
        </el-form-item>
        <el-form-item label="管理员权限" style="width: 200px;">
            <el-select v-model="topChosen" class="m-2" placeholder="权限等级">
                <el-option v-for="item in weatherOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
        </el-form-item>
        <el-form-item style="width: 200px;">
            <el-button @click="search" type="primary" plain style="width: 200px;">查询</el-button>
        </el-form-item>
    </el-form>


    <el-divider>
        <el-icon><star-filled /></el-icon>
    </el-divider>

    <el-button type="primary" plain :onclick="onAddItem" class="addbtn">
        <el-icon>
            <Edit />
        </el-icon>
        <span>添加新的后台管理员</span>
    </el-button>
    <div id="avapart" v-if="istop == 3">
        <el-avatar id="ava" :src="avaUrl" />
        <div>
            <span id="spanpart">您暂时没有查看权限</span>
        </div>
    </div>
    <!-- 展示表格 -->
    <el-table :data="tableData" style="width: 100%; margin: 0 auto;  " v-else>
        <el-table-column prop="admineid" label="管理员id" width="" />
        <el-table-column prop="adminename" label="管理员" width="" />
        <el-table-column prop="pwd" label="管理员密码" width="" type="password" />
        <el-table-column prop="leval" label="管理员权限" width="" />

        <el-table-column fixed="right" label="操作" width="" style="text-align: left;">
            <template #default="scope">
                <el-button link type="primary" size="small" style="margin-left: 0;"
                    v-if="tableData1[scope.$index].istop > istop"
                    @click.prevent="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button link type="primary" size="small" style="margin-left: 0;"
                    v-if="tableData1[scope.$index].istop > istop"
                    @click.prevent="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>



    <el-pagination background layout="prev, pager, next" :current-page="page" @update:current-page="handleCurrentChange"
        :total="total" :page-size="size" style="bottom: 0; text-align: center;margin-left: 45%;margin-top: 5px;" />

    <!-- Form -->
    <el-dialog v-model="dialogaddFormVisible" title="添加管理员" :destroy-on-close="true">
        <el-form :model="addform">
            <el-form-item label="管理员" :label-width="formLabelWidth">
                <el-input v-model="addform.adminename" autocomplete="off" />
            </el-form-item>
            <el-form-item label="密码" :label-width="formLabelWidth">
                <el-input v-model="addform.adminepwd" autocomplete="off" />
            </el-form-item>
            <el-form-item label="权限" :label-width="formLabelWidth">
                <el-select v-model="addform.istop" placeholder="请选择权限等级">
                    <el-option label="权限等级1(最高权限)" value=1 />
                    <el-option label="权限等级2" value=2 />
                    <el-option label="权限等级3" value=3 />
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="closeadd">取消</el-button>
                <el-button type="primary" @click="onAddInfo">
                    确定
                </el-button>
            </span>
        </template>
    </el-dialog>

    <el-dialog v-model="dialogFormVisible" title="编辑" :destroy-on-close="true">
        <el-form :model="form">
            <el-form-item label="管理员" :label-width="formLabelWidth">
                <el-input v-model="form.adminename" autocomplete="off" />
            </el-form-item>
            <el-form-item label="密码" :label-width="formLabelWidth">
                <el-input v-model="form.adminepwd" autocomplete="off" />
            </el-form-item>
            <el-form-item label="权限" :label-width="formLabelWidth">
                <el-select v-model="form.istop" placeholder="请选择权限等级">
                    <el-option label="权限等级1(最高权限)" value=1 />
                    <el-option label="权限等级2" value=2 />
                    <el-option label="权限等级3" value=3 />
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="onSaveInfo">
                    提交
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script setup>
import {
    StarFilled,
    EditPen,
    Edit,
    Promotion,
    Grape,
    Pear,
} from '@element-plus/icons-vue';
import dayjs from 'dayjs';
import { ElMessage, ElMessageBox } from 'element-plus';
import axios from 'axios';
const formLabelWidth = '240px';
</script>
<script>

export default {
    setup() {

    },
    data() {
        return {
            topChosen: '',
            istop: 0,
            avaUrl: require('../../public/1.jpg'),
            baseURL: 'http://localhost:8080',
            fileList: [],
            fileArray: [],
            editfileArray: [],
            editfileList: [],

            dialogFormVisible: false,
            dialogaddFormVisible: false,
            showSearch: false,
            tableData: [],
            tableData1: [],
            tableDataStore: [],
            tableimgList: [],
            tableimg: '',
            weatherOptions: [
                {
                    value: 1,
                    label: "权限等级1(最高权限)",
                },
                {
                    value: 2,
                    label: '权限等级2',
                },
                {
                    value: 3,
                    label: '权限等级3',
                },
            ],
            form: {
                adminename: '',
                adminepwd: '',
                istop: '',
                pwd: '',
            },
            formInline: {
                adminename: '',
                adminepwd: '',
                istop: '',
                pwd: '',
            },
            checkformInline: {
                adminename: '',
                adminepwd: '',
                istop: '',
                pwd: '',
            },
            addform: {
                adminename: '',
                adminepwd: '',
                istop: '',
                pwd: '',
            },
            searchForm: [],
            categoryChange: 9,
            wet: [],
            addcategoryChange: 9,
            weatherChange: 9,
            placeChange: 9,
            page: 1, // 当前页码
            total: 0, // 总条数
            size: 10, // 每页的数据条数
            reallyTable: [],
            selData: [],
        }

    },
    methods: {
        init() {
            let token = window.localStorage.getItem('token')
            // console.log(window.localStorage.getItem('token'));
            console.log(token);
            axios({
                method: 'post',
                url: 'http://localhost:8080/administrator/getall',
                withCredentials: true,
                data: { token: token }
            }).then(res => {
                if (res.data.state) {
                    if (res.data.istop == 1) {
                        console.log(11111);
                        console.log(this.tableData);
                        this.istop = 1
                    } else if (res.data.istop == 2) {
                        console.log(22222222);
                        this.istop = 2
                    } else {
                        this.istop = 3
                    }
                    console.log(res.data);
                }
                this.tableDataStore = res.data.data
                this.selData = res.data.data
                for (let i = 0; i < this.selData.length; i++) {
                    this.selData[i].pwd = "********"
                    if (this.selData[i].istop == 1) {
                        this.selData[i].leval = "权限等级" + this.selData[i].istop + "(最高权限)"
                    } else if (this.selData[i].istop == 2) {
                        this.selData[i].leval = "权限等级" + this.selData[i].istop
                    } else {
                        this.selData[i].leval = "权限等级" + this.selData[i].istop
                    }
                }
                this.tableData = this.selData.slice(
                    (this.page - 1) * this.size,
                    this.page * this.size
                );
                this.tableData1 = this.tableDataStore.slice(
                    (this.page - 1) * this.size,
                    this.page * this.size
                );
            })
        },
        search() {
            console.log('000000');
            let arr = []
            for (let i = 0; i < this.tableDataStore.length; i++) {
                if (String(this.tableDataStore[i].adminename).includes(this.checkformInline.adminename) &&
                    String(this.tableDataStore[i].istop).includes(this.topChosen)) {
                    arr.push(this.tableDataStore[i])
                }
            }
            console.log(arr);
            console.log(arr);
            this.tableData = arr.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            this.selData = arr;
            this.total = arr.length;
        },

        handleRemove(file, fileList) {
            this.fileList = fileList
        },
        handlePreview(file) {
            console.log(file);
        },
        //删除表格行
        handleDelete(index, row) {
            console.log(index, row);
            console.log(row.cropsid);//可以获取到
            ElMessageBox.confirm(
                '确认删除本项吗？删除后无法找回:-0',
                'Warning',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
                .then(() => {
                    axios({
                        method: 'post',
                        url: 'http://localhost:8080/tags/deltagsbackend',
                        withCredentials: true,
                        data: {
                            cropsid: row.cropsid,
                        },
                    }).then(res => {
                        // if (res.data.data.code == 200) {
                        this.init()
                        ElMessage({
                            type: 'success',
                            message: '删除成功',
                        })

                        // }

                    })
                })
                .catch(() => {
                    ElMessage({
                        type: 'info',
                        message: '取消删除操作',
                    })
                })
        },
        //编辑表格行
        handleEdit(index, row) {
            console.log(index, row);
            this.dialogFormVisible = true;
            this.form = this.tableData1[index]
        },
        async onSaveInfo() {
            if (this.form.adminename != '' && this.form.adminepwd != '' && this.form.istop != '') {
                console.log(this.form);
                axios({
                    method: 'post',
                    url: 'http://localhost:8080/administrator/edit',
                    withCredentials: true,
                    data: {
                        form: this.form
                    },
                }).then(res => {
                    console.log(res);
                    this.init()
                })
            } else {
                alert("存在未输入项，请检查")
            }


            this.dialogFormVisible = false;

        },
        //增添项
        onAddItem() {
            this.dialogaddFormVisible = true;
            console.log('additem');
        },
        async onAddInfo() {
            if (this.addform.adminename != '' && this.addform.adminepwd != '' && this.addform.istop != '') {
                axios({
                    method: 'post',
                    url: 'http://localhost:8080/administrator/addadmin',
                    withCredentials: true,
                    data: {
                        data: this.addform
                    },
                }).then(res => {
                    this.init()
                    console.log("additem");
                })
            } else {
                alert("存在未输入项，请检查")
            }
            this.dialogaddFormVisible = false;
        },
    },
    mounted() {
        //页面加载完成后，将数据库内的内容赋给定义的data变量
        this.init()
    }
}
</script>
<style scoped>
#sidepart {
    width: 100%;
    position: absolute;
    top: 5px;
    font-family: '华文中宋';
}

#mainpart {
    width: 80%;
    position: absolute;
    top: 20px;
    left: 260px;
    font-family: '华文中宋';
}

#spanpart {
    font-size: 30px;
    font-family: '华文中宋';
}

/* dialog */
.el-button--text {
    margin-right: 15px;
}

.el-select {
    width: 300px;
}

.el-input {
    width: 300px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}

#spanlittlepart {
    font-size: 15px;
    color: cadetblue;
    font-family: '华文中宋';
}

.spantext {
    margin-right: 20px;
}

.addbtn {
    width: 80%;
    margin-bottom: 20px;
    font-family: '华文中宋';
    position: relative;
    left: -50px;
}
</style>